package com.twu.biblioteca.users;

import com.twu.biblioteca.items.Book;
import com.twu.biblioteca.items.Movie;
import com.twu.biblioteca.library.ILibraryItemCollector;

import java.util.HashMap;

/**
 * @author Alvi Jonathan Sait
 */
public class User implements ILibraryItemCollector {

    private final String username;
    private final String password;
    private final String name;
    private final String emailAddress;
    private final String phoneNumber;
    private final HashMap<String, Book> checkedOutBooks;
    private final HashMap<String, Movie> checkedOutMovies;

    public User(String username, String password, String name, String emailAddress, String phoneNumber) {
        this.username = username;
        this.password = password;
        this.name = name;
        this.emailAddress = emailAddress;
        this.phoneNumber = phoneNumber;
        this.checkedOutBooks = new HashMap<String, Book>();
        this.checkedOutMovies = new HashMap<String, Movie>();
    }

    @Override
    public void checkoutBook(Book book) {
        checkedOutBooks.put(book.getISBN(), book);
        book.setAvailable(false);
    }

    @Override
    public void returnBook(Book book) {
        checkedOutBooks.remove(book.getISBN());
        book.setAvailable(true);
    }

    /**
     * Only returns a copy of the map.
     */
    @Override
    public HashMap<String, Book> getBooks() {
        return new HashMap<String, Book>(checkedOutBooks);
    }

    @Override
    public void checkoutMovie(Movie movie) {
        checkedOutMovies.put(movie.getMovieId(), movie);
        movie.setAvailable(false);
    }

    /**
     * Only returns a copy of the map.
     */
    @Override
    public HashMap<String, Movie> getMovies() {
        return new HashMap<String, Movie>(checkedOutMovies);
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getName() {
        return name;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * Two users are considered equal if they have the same username.
     *
     * @param o - Other object
     * @return <b>true</b> if same username; <b>false</b> otherwise
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        return !(username != null ? !username.equals(user.username) : user.username != null);
    }

    @Override
    public int hashCode() {
        return username != null ? username.hashCode() : 0;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(String.format("%-15s: %s", "Name", name)).append(System.lineSeparator());
        builder.append(String.format("%-15s: %s", "Email Address", emailAddress)).append(System.lineSeparator());
        builder.append(String.format("%-15s: %s", "Phone Number", phoneNumber));
        return builder.toString();
    }
}
